package ui.history;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import tool.DateChooserJButton;
import tool.MyButton;
import tool.MyLabel;
import vo.UserVo;
import vo.销售明细vo;
import blservice.history.SaleLogBLService;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import client.boot.BLServiceManager;

public class SaleLogPanel extends JPanel {
	private static final long serialVersionUID = -9016884218956444023L;

	@SuppressWarnings("unused")
	private UserVo user;
	
	private SaleLogBLService bl;
	private JTextField goodName;
	private JTable table;
	private List<销售明细vo> list;
	private DateChooserJButton timeFrom;
	private DateChooserJButton timeTo;
	private JTextField client;
	private JTextField operator;
	private JTextField storage;

	public SaleLogPanel(UserVo user) {
		try {
			bl=BLServiceManager.getSaleLogBLService(null);
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e1) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n"+e1.getMessage());
			e1.printStackTrace();
		}
		setLayout(null);
		this.setOpaque(false);
		setSize(646, 465);
		JLabel lblNewLabel = new MyLabel("起始时间");
		lblNewLabel.setBounds(255, 10, 65, 15);
		add(lblNewLabel);

		timeFrom = new DateChooserJButton();
		timeFrom.setBounds(323, 8, 78, 19);
		add(timeFrom);

		JLabel label = new MyLabel("\u7ED3\u675F\u65F6\u95F4");
		label.setBounds(453, 10, 70, 15);
		add(label);

		timeTo = new DateChooserJButton();
		timeTo.setBounds(518, 8, 78, 19);
		add(timeTo);

		JLabel lblNewLabel_1 = new MyLabel("商品名");
		lblNewLabel_1.setBounds(50, 10, 70, 15);
		add(lblNewLabel_1);

		JLabel label_1 = new MyLabel("\u5BA2\u6237");
		label_1.setBounds(50, 35, 70, 15);
		add(label_1);

		client = new JTextField();
		client.setBounds(113, 35, 78, 19);
		add(client);
		client.setColumns(10);

		JLabel label_2 = new MyLabel("\u4E1A\u52A1\u5458");
		label_2.setBounds(255, 35, 70, 15);
		add(label_2);

		operator = new JTextField();
		operator.setBounds(323, 35, 78, 19);
		add(operator);
		operator.setColumns(10);

		JLabel label_3 = new MyLabel("\u4ED3\u5E93");
		label_3.setBounds(453, 35, 70, 15);
		add(label_3);
		
		storage=new JTextField();
		storage.setBounds(518,35,78,19);
		add(storage);
		storage.setColumns(10);

		JButton selectButton = new MyButton("Graphics/Button65-23.png");
        selectButton.setText("筛选");
		selectButton.addActionListener(e->select());

		selectButton.setBounds(531, 69, 65, 23);
		add(selectButton);

		goodName = new JTextField();
		goodName.setBounds(113, 8, 78, 19);
		add(goodName);
		goodName.setColumns(10);

		table = new JTable();
		JScrollPane jsp=new JScrollPane(table);
		jsp.setBounds(50, 117, 548, 266);
		add(jsp);
		
		JButton exportButton = new MyButton("Graphics/Button93-23.png");
		exportButton.setText("导出");
		exportButton.addActionListener(new tool.JTableExportListener(table, "销售情况表", ""));
		exportButton.setBounds(503, 393, 93, 23);
		add(exportButton);
		
		JButton showAllButton = new MyButton("Graphics/Button93-23.png");
		showAllButton.setText("显示所有");
		showAllButton.addActionListener(e->getAll());
		showAllButton.setBounds(430, 69, 93, 23);
		add(showAllButton);

		getAll();
	}
	
	private void getAll(){
		try {
			list=bl.get();
			display();
		} catch (RemoteException e1) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e1.getMessage());
			e1.printStackTrace();
		}		
	}
	
	private void select(){
		if(timeFrom.getDate().getTime()>timeTo.getDate().getTime()){
			JOptionPane.showMessageDialog(this, "开始时间不能大于结束时间！");
			return;
		}
		销售明细vo from=new 销售明细vo();
		销售明细vo to=new 销售明细vo();
		from.set时间(timeFrom.getDate().getTime());
		to.set时间(timeTo.getDate().getTime()+60*60*24*1000-1);
		if(!goodName.getText().equals("")){
			from.set商品名(goodName.getText());
			to.set商品名(goodName.getText());
		}
		if(!client.getText().equals("")){
			from.set客户(client.getText());
			to.set客户(client.getText());
		}
		if(!operator.getText().equals("")){
			from.set业务员(operator.getText());
			to.set业务员(operator.getText());
		}
		if(!storage.getText().equals("")){
			from.set仓库(storage.getText());
			to.set仓库(storage.getText());
		}
		try {
			list=bl.get(from, to);
			display();
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void display() {
		/*
		table.setModel(new DefaultTableModel(new Object[][] {
				{ "时间", "商品名", "型号", "数量", "单价", "总额" },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null },
				{ null, null, null, null, null, null }, }, new String[] { "时间",
				"商品名", "型号", "数量", "单价", "总额" }));
				*/
		Vector<Vector<String>> data=new Vector<Vector<String>>();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		for(销售明细vo o:list){
			Vector<String> item=new Vector<String>();
			item.add(df.format(new Date(o.get时间())));
			item.add(o.get商品名());
			item.add(o.get型号());
			item.add(Integer.toString(o.get数量()));
			item.add(tool.LongStringTransformer.LongToString(o.get单价()));
			item.add(tool.LongStringTransformer.LongToString(o.get总额()));
			data.add(item);
		}
		Vector<String> columnNames=new Vector<String>();
		columnNames.add("时间");
		columnNames.add("商品名");
		columnNames.add("型号");
		columnNames.add("数量");
		columnNames.add("单价");
		columnNames.add("总额");
		table.setModel(new DefaultTableModel(data, columnNames));
	}
	
}
